﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ClientLib;

namespace TinyCClient
{
    public partial class TextTalk : Form
    {
        TinyMsgToolLib.TinyMsgInterface TCPclient;
        TinyClientRuntime m_runtime;

        public void setTCPClient(TinyMsgToolLib.TinyMsgInterface tcp)
        {

            TCPclient = tcp;
        }
        public void setRuntime(TinyClientRuntime runtime)
        {

            m_runtime = runtime;
        }

        public TextTalk()
        {
            InitializeComponent();
        }

        public void ProcessReceivedMsg()
        {
            //Asynced Msg Interface
            switch (TCPclient.getMsgIndex())
            {
                case GlobalDefine.CLIENT_ACTION:
                    {
                        switch (TCPclient.getMsgItem())
                        {
                            case GlobalDefine.CLIENT_SEND_TXTMSG:
                                {
                                    string source,target, msg,region,moduleName,moduleType;
                                    moduleName = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_MODULENAME);
                                    moduleType = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_MODULETYPE);
                                    region = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_REGION);
                                    source = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_USERNAME);
                                    target = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_TARGET);
                                    msg = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_MSG);
                                    this.outputTextBox.Text += source + ":" + msg + "\r\n";

                                    TCPclient.clear();
                                    TCPclient.setMsgIndex(GlobalDefine.CLIENT_ACTION);
                                    TCPclient.setMsgItem(GlobalDefine.CLIENT_SEND_TXTMSG_SUCC);
                                    TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_MODULENAME, moduleName);
                                    TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_MODULETYPE, moduleType);
                                    TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_USERNAME, source);
                                    TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_REGION, region);
                                    TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_TARGET, target);
                                    TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_MSG, msg);
                                    TCPclient.send();
                                }
                                break;
                            case GlobalDefine.CLIENT_SEND_TXTMSG_FINISHED:
                                {
                                    string source, msg;
                                    source = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_USERNAME);
                                    //target = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_TARGET);
                                    msg = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_MSG);
                                    this.outputTextBox.Text += source + ":" + msg + "\r\n";
                                }
                                break;
                            default:
                                break;
                        }
                    }
                    break;
                default:
                    break;
            }
            return;
        }

        private void SendMsg_Click(object sender, EventArgs e)
        {
            //this.outputTextBox.Text += this.inputRichTextBox.Text + "\r\n";
            if (TCPclient == null)
            {
                MessageBox.Show("Error occurred!,please Re login");
            }
            TCPclient.setMsgIndex(GlobalDefine.CLIENT_ACTION);
            TCPclient.setMsgItem(GlobalDefine.CLIENT_SEND_TXTMSG);
            TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_MODULENAME, m_runtime.g_Config.m_ModuleName);
            TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_MODULETYPE, m_runtime.g_Config.m_ModuleType);
            TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_USERNAME, m_runtime.g_Config.m_userName);
            TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_REGION, m_runtime.g_Config.m_region);
            TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_TARGET, Text);
            TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_MSG, inputRichTextBox.Text);
            TCPclient.send();
            this.inputRichTextBox.Text = "";
        }

        private void CloseDia_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void outputTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
            this.inputRichTextBox.Text += e.KeyChar;
            this.inputRichTextBox.Focus();
        }

        private void TextTalk_Load(object sender, EventArgs e)
        {
            toolStripButtonUserInfo.Text = Text;
        }

        private void toolStripButtonUserInfo_Click(object sender, EventArgs e)
        {

        }

        private void TextHistoryButton_Click(object sender, EventArgs e)
        {
            TextHistoryForm historyForm = new TextHistoryForm();
            historyForm.Show();
        }

        private void buttonMakeCall_Click(object sender, EventArgs e)
        {

        }
    }
}